package cn.good.yan.t1;

import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 1. 测试 IOC 实体
 * @author shengren.yan
 * @create 2023-09-06
 */
public class TestUser {

    private Logger logger = LoggerFactory.getLogger(TestUser.class);

    @Test
    public void test(){

        logger.info("执行成功");
//        doBeanC1();
        doBeanC2();
        doBeanC3();

        // 反射创建对象
//        try {
//            Class clazz = Class.forName("cn.good.yan.t1.User");
////            Object obj = clazz.newInstance(); // 过时
//            User user2 = (User) clazz.getDeclaredConstructor().newInstance();
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }

    }

    /**
     * 通过Beanid注入，创建对象
     */
    private static void doBeanC1() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        // user ： 是bean值
        User user = (User) ac.getBean("user");
        user.sayHello();
    }

    /**
     * 通过set get方法注入，创建对象
     */
    private static void doBeanC2() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        User user = ac.getBean("user2", User.class);
        System.out.println(user);
    }

    /**
     * 通过构造器注入，创建对象
     */
    private static void doBeanC3() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        User user = ac.getBean("user3", User.class);
        System.out.println(user);
    }

}
